package sundun.sharding.factory;


import org.apache.ibatis.mapping.SqlCommandType;
import sundun.sharding.config.TableConfiguration;
import sundun.sharding.execution.ExecutionStrategy;
import sundun.sharding.execution.impl.WriteExecutionStrategy;
import sundun.sharding.execution.impl.ReaderExecutionStrategy;

import java.util.HashMap;
import java.util.Map;

/**
 * 执行器工厂
 * @author: zxl
 * @date: 2024/8/11 19:22
 */
public class ExecutionFactory {

    private final static Map<SqlCommandType, ExecutionStrategy> executionStrategyMap = new HashMap<>(4);


    public ExecutionFactory(SqlSessionFactoryWrapper sqlSessionFactory, Map<String, TableConfiguration> tableConfigurations) throws ClassNotFoundException {
        WriteExecutionStrategy writeExecutionStrategy = new WriteExecutionStrategy(sqlSessionFactory,tableConfigurations);
        executionStrategyMap.put(SqlCommandType.INSERT, writeExecutionStrategy);
        executionStrategyMap.put(SqlCommandType.DELETE, writeExecutionStrategy);
        executionStrategyMap.put(SqlCommandType.UPDATE, writeExecutionStrategy);
        executionStrategyMap.put(SqlCommandType.SELECT,new ReaderExecutionStrategy(sqlSessionFactory,tableConfigurations));
    }

    public ExecutionStrategy getExecutionStrategy(SqlCommandType sqlCommandType){
        return executionStrategyMap.get(sqlCommandType);
    }





}
